<template>
  <div class="employee-list">
    <div style="margin-bottom: 10px;">
      <el-input placeholder="查询姓名" style="width: 200px;" v-model="username"></el-input>
      <el-button type="primary" @click="load(1)">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>员工列表</span>
        <el-button style="float: right;" type="primary" size="mini" @click.native="handleAdd">添加用户</el-button>
      </div>
      <el-table :index="indexMethod" :data="eusers" style="width: 100%" stripe :header-cell-style="{ backgroundColor: 'aliceblue' , color: '#666'}" @selection-change="handleSelectionChange">
        <el-table-column type="index" label="员工ID" width="200"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="department" label="部门"></el-table-column>
        <el-table-column prop="position" label="职位"></el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 10px 0;">
        <el-pagination @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[100, 200, 300, 400]" :page-size="pageSize" layout="total, prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog title="添加用户" :visible.sync="userFormVisible" width="40%" center>
      <el-form :model="form" :rules="rules" label-width="80px" ref="formRef">
        <el-form-item label="姓名" prop="username">
          <el-input prefix-icon="el-icon-user" v-model="form.username" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input prefix-icon="el-icon-office-building" v-model="form.department" placeholder="部门"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input prefix-icon="el-icon-postcard" v-model="form.position" placeholder="职位"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "EuserView",
  data() {
    return {
      eusers: [],
      pageNum: 1, // 当前页码
      pageSize: 5,  // 每页显示的个数
      username: '',
      total: 0,
      userFormVisible: false,
      form: {},
      rules: {
        // 必须跟上面输入账号的 prop="username" 一样，而且必须在自定的  user 对象里有 username

        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请输入部门', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请输入职位', trigger: 'blur' }
        ],
      },
    };
  },
  created() {
    this.load()
  },
  mounted(){
    
  },
  methods: {
    handleSelectionChange(){

    },
    //删除数据
    handleDelete(id) {
      this.$confirm('你确认删除吗？', '确认删除', { type: "warning" }).then(response => {
        this.$request.delete('/euser/delete/' + id).then(res => {
          if (res.code === '200') {
            this.$message.success('删除成功')
            // 更新表格数据
            this.load(1)
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { })
    },
    // 编辑数据
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      // 打开弹窗
      this.userFormVisible = true
    },
    // 新增数据
    handleAdd() {
      // 新增数据的时候清空数据
      this.form = {}
      // 打开对话框
      this.userFormVisible = true
    },
    save() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/euser/update' : '/euser/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {
              // 表示成功
              this.$message.success('保存成功')
              this.load(1)
              this.userFormVisible = false
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    reset() {
      this.username = ''
      this.load()
    },
    load(pageNum) {
      if (pageNum) this.pageNum = pageNum

      this.$request.get('/euser/selectByPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
        }
      }).then(res => {
        this.eusers = res.data.records
        this.total = res.data.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    indexMethod(index) {
      // 自定义行号，从1开始
      return index + 1;
    }
  },
};
</script>

<style>

</style>
